//
//  WXPOrderDetailTopMoneyView.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/18.
//

#import "WXPOrderDetailTopMoneyView.h"

@implementation WXPOrderDetailTopMoneyView

#pragma mark - lazy
- (UIView *)viewBack{
    if (!_viewBack) {
        _viewBack = [UIView new];
        _viewBack.backgroundColor = COLOR_WHITE;
        _viewBack.width = SCREEN_WIDTH-LEFT_MARDIN*2;
    }
    return _viewBack;
}
- (UIImageView *)imageLogo{
    if (_imageLogo == nil) {
        _imageLogo = [UIImageView new];
        _imageLogo.widthHeight = XY(36, 36);
        [GlobalMethod setRoundView:_imageLogo color:[UIColor clearColor] numRound:8 width:0];
    }
    return _imageLogo;
}
- (UILabel *)labelTitle{
    if (_labelTitle == nil) {
        _labelTitle = [UILabel new];
        _labelTitle.textColor = COLOR_BLACK;
        _labelTitle.font = [UIFont boldSystemFontOfSize:14];
        _labelTitle.numberOfLines = 1;
    }
    return _labelTitle;
}
- (UILabel *)labelState{
    if (_labelState == nil) {
        _labelState = [UILabel new];
        _labelState.textColor = COLOR_CONTENT;
        _labelState.fontNum = 12;
        _labelState.textAlignment = NSTextAlignmentCenter;
    }
    return _labelState;
}
- (UILabel *)labelMoneyTitle{
    if (_labelMoneyTitle == nil) {
        _labelMoneyTitle = [UILabel new];
        _labelMoneyTitle.textColor = COLOR_BLACK;
        _labelMoneyTitle.fontNum = 12;
        [_labelMoneyTitle fitTitle:@"Monto del préstamo($)" variable:0];
    }
    return _labelMoneyTitle;
}
- (UILabel *)labelMoney{
    if (_labelMoney == nil) {
        _labelMoney = [UILabel new];
        _labelMoney.textColor = COLOR_BLACK;
        _labelMoney.font = [UIFont boldSystemFontOfSize:20];
    }
    return _labelMoney;
}
- (UILabel *)labelTimeTitle{
    if (_labelTimeTitle == nil) {
        _labelTimeTitle = [UILabel new];
        _labelTimeTitle.textColor = COLOR_BLACK;
        _labelTimeTitle.fontNum = 12;
        [_labelTimeTitle fitTitle:@"Plazo del préstamo(Días)" variable:0];
    }
    return _labelTimeTitle;
}
- (UILabel *)labelTime{
    if (_labelTime == nil) {
        _labelTime = [UILabel new];
        _labelTime.textColor = COLOR_BLACK;
        _labelTime.font = [UIFont boldSystemFontOfSize:20];
    }
    return _labelTime;
}
- (NSString *)stateStr
{
    switch ([self.model.status integerValue]) {
        case 90:
        case 100:
        {
            // 审核中和审核通过
            return @"En revisión";
        }
            break;
        case 150:
        {
            // 放款中
            return @"Depositando";
        }
            break;
        case 170:
        {
            // 放款成功
            return @"Reembolso pendiente";
        }
            break;
        case 180:
        {
            // 订单逾期
            return @"Reembolso atrasado";
        }
            break;
        case 200:
        {
            // 订单结清(已还)
            return @"Reembolsado";
        }
            break;
            
        default:
        {
            return @"";
        }
            break;
    }
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, 0);
        self.backgroundColor = [UIColor clearColor];
        [self addSubView];
    }
    return self;
}

- (void)addSubView{
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.imageLogo];
    [self.viewBack addSubview:self.labelTitle];
    [self.viewBack addSubview:self.labelState];
    [self.viewBack addSubview:self.labelMoneyTitle];
    [self.viewBack addSubview:self.labelMoney];
    [self.viewBack addSubview:self.labelTimeTitle];
    [self.viewBack addSubview:self.labelTime];
    [self resetView:nil];
}

#pragma mark - 刷新View
- (void)resetView:(ModelOrderDetail *)model{
    self.model = model;
    UIImage * image = [UIImage new];
    if(isStr(readStr(model.slogan))){
        NSData *imageData = [[NSData alloc] initWithBase64EncodedString:model.slogan options:NSDataBase64DecodingIgnoreUnknownCharacters];
        image = [UIImage imageWithData:imageData];
    }
    self.imageLogo.image = image;
    self.imageLogo.leftTop = XY(LEFT_MARDIN, LEFT_MARDIN);
    
    [self.labelTitle fitTitle:readStr(model.productName) variable:0];
    self.labelTitle.leftCenterY = XY(self.imageLogo.right+12, self.imageLogo.centerY);
    
    self.labelState.backgroundColor = (model.status.integerValue == 180) ? [UIColor colorWithHexString:@"E84A55" alpha:0.2] : COLOR_LINE;
    self.labelState.textColor = (model.status.integerValue == 180) ? COLOR_RED : COLOR_CONTENT;
    [self.labelState fitTitle:[NSString stringWithFormat:@"%@",self.stateStr] variable:0];
    self.labelState.widthHeight = XY(self.labelState.width+20+20, 20);
    [GlobalMethod setRoundView:self.labelState color:[UIColor clearColor] numRound:20/2 width:0];
    self.labelState.leftCenterY = XY(self.labelTitle.right+10, self.imageLogo.centerY);
    
    self.labelMoneyTitle.leftTop = XY(self.imageLogo.left, self.imageLogo.bottom+LEFT_MARDIN);
    [self.labelMoney fitTitle:[NSString stringWithFormat:@"%@",readStr(model.loanAmount)] variable:0];
    self.labelMoney.leftTop = XY(self.imageLogo.left, self.labelMoneyTitle.bottom+12);
    
    self.labelTimeTitle.rightCenterY = XY(self.viewBack.width-self.imageLogo.left, self.labelMoneyTitle.centerY);
    [self.labelTime fitTitle:strDotF(model.term) variable:0];
    self.labelTime.rightCenterY = XY(self.labelTimeTitle.right, self.labelMoney.centerY);
    
    self.viewBack.height = self.labelMoney.bottom+LEFT_MARDIN;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.viewBack.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(16, 16)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = path.CGPath;
    self.viewBack.layer.mask = maskLayer;
    self.viewBack.centerXTop = XY(self.width/2, 0);
    
    self.height = self.viewBack.bottom;
}

- (void)resetViewWithModelOrderDetail:(ModelOrderDetail *)model modelPostpone:(ModelPostpone *)modelPostpone{
    self.model = model;
    UIImage * image = [UIImage new];
    if(isStr(readStr(model.slogan))){
        NSData *imageData = [[NSData alloc] initWithBase64EncodedString:model.slogan options:NSDataBase64DecodingIgnoreUnknownCharacters];
        image = [UIImage imageWithData:imageData];
    }
    self.imageLogo.image = image;
    self.imageLogo.leftTop = XY(LEFT_MARDIN, LEFT_MARDIN);
    
    [self.labelTitle fitTitle:readStr(model.productName) variable:0];
    self.labelTitle.leftCenterY = XY(self.imageLogo.right+12, self.imageLogo.centerY);
    
    self.labelState.backgroundColor = (model.status.integerValue == 180) ? [UIColor colorWithHexString:@"E84A55" alpha:0.2] : COLOR_LINE;
    self.labelState.textColor = (model.status.integerValue == 180) ? COLOR_RED : COLOR_CONTENT;
    [self.labelState fitTitle:[NSString stringWithFormat:@"%@>",self.stateStr] variable:0];
    self.labelState.widthHeight = XY(self.labelState.width+20+20, 20);
    [GlobalMethod setRoundView:self.labelState color:[UIColor clearColor] numRound:20/2 width:0];
    self.labelState.leftCenterY = XY(self.labelTitle.right+10, self.imageLogo.centerY);
    
    self.labelMoneyTitle.leftTop = XY(self.imageLogo.left, self.imageLogo.bottom+LEFT_MARDIN);
    [self.labelMoney fitTitle:[NSString stringWithFormat:@"%@",readStr(modelPostpone.amount)] variable:0];
    self.labelMoney.leftTop = XY(self.imageLogo.left, self.labelMoneyTitle.bottom+12);
    
    self.labelTimeTitle.rightCenterY = XY(self.viewBack.width-self.imageLogo.left, self.labelMoneyTitle.centerY);
    [self.labelTime fitTitle:readStr(modelPostpone.delayExpiryTime) variable:0];
    self.labelTime.rightCenterY = XY(self.labelTimeTitle.right, self.labelMoney.centerY);
    
    self.viewBack.height = self.labelMoney.bottom+LEFT_MARDIN;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.viewBack.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(16, 16)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = path.CGPath;
    self.viewBack.layer.mask = maskLayer;
    self.viewBack.centerXTop = XY(self.width/2, 0);
    
    self.height = self.viewBack.bottom;
}

- (void)resetStateLabel:(ModelBtn *)model{
    [self.labelMoneyTitle fitTitle:readStr(model.subTitle) variable:0];
    self.labelMoneyTitle.leftCenterY = self.labelMoneyTitle.leftCenterY;
    
    [self.labelTimeTitle fitTitle:readStr(model.content) variable:0];
    self.labelTimeTitle.rightCenterY = XY(self.viewBack.width-self.imageLogo.left, self.labelMoneyTitle.centerY);
    
    self.labelState.backgroundColor = COLOR_LINE;
    self.labelState.textColor = COLOR_CONTENT;
    [self.labelState fitTitle:readStr(model.title) variable:0];
    self.labelState.widthHeight = XY(self.labelState.width+20+20, 20);
    [GlobalMethod setRoundView:self.labelState color:[UIColor clearColor] numRound:20/2 width:0];
    self.labelState.leftCenterY = XY(self.labelTitle.right+10, self.imageLogo.centerY);
}





@end
